package com.relatev.minecraft.TRAttribute;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;

public class TRAConfigManager {

    private File AttributeConfigFolder;
    private File GobalConfigFile;
    public final YamlConfiguration GobalConfig;

    public TRAConfigManager() {
        ConfigurationSerialization.registerClass(AttributeValue.class);

        GobalConfigFile = new File(TRAttribute.mainPlugin.getDataFolder(), "GobalConfig.yml");
        if (GobalConfigFile.exists() == false) {
            GobalConfigFile.getParentFile().mkdirs();
            try {
                GobalConfigFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(TRAConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        GobalConfig = YamlConfiguration.loadConfiguration(GobalConfigFile);
        if (GobalConfig.getInt("Version") != 300) {
            GobalConfig.set("Version", 300);
            GobalConfig.set("CacheClearTickPeriod", 20);
            try {
                GobalConfig.save(GobalConfigFile);
            } catch (IOException ex) {
                Logger.getLogger(TRAConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        AttributeConfigFolder = new File(TRAttribute.mainPlugin.getDataFolder(), "AttributeConfig");
        if (AttributeConfigFolder.exists() == false) {
            AttributeConfigFolder.mkdirs();
        }
    }

    public File getGobalConfigFile() {
        return GobalConfigFile;
    }

    public File getAttributeConfigFile(String id) {
        return new File(AttributeConfigFolder, id + ".yml");
    }
}
